﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Microsoft.TV.TVControls;
using IPTV.service;
using IPTV.module;
using IPTV.util;
using System.Threading;
using Microsoft.TV.TVControls.Actions;
using System.Text;
using System.Xml;

public partial class page_primaryEnglish : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        String subject = "";
        string subjectId = "e484649f-dd42-4009-97e7-ab372ad45d52";
        String categorykind = "CK_TGSG_ZT_YW";
        if (Request.QueryString.HasKeys() && Request.QueryString["subject"] != null)
        {
            subject = Request.QueryString["subject"].ToString().Trim();
            if(subject.Equals("E"))
            {
               subject="英语";
               subjectId = "02429209-374f-4ce6-a6bf-6b8b73d8b013";
               categorykind = "CK_TGSG_ZT_YY";
            }
            else if(subject.Equals("C"))
            {
                subject="语文";
                subjectId = "e484649f-dd42-4009-97e7-ab372ad45d52";
                categorykind = "CK_TGSG_ZT_YW";
            }
            else
            {
               subject="数学";
               subjectId = "8aaabfad-3cd7-46f6-807c-ce166fda0fd4";
               categorykind = "CK_TGSG_ZT_SX";
            }
        }

        Session[CommonUtil.SESSION_SUBJECT_ID] = subjectId;
        Thread logThread = new Thread(logAction);
        logThread.Start();

        subjectNameLbl.Text = "小学" + subject;
        SubjectService subjectService = new SubjectService();
        List<me_zt_baseinfo>  ztList = subjectService.getZtByCategorykind(categorykind);
        if (null != ztList && ztList.Count != 0)
        {
            ZtToXML(Request.PhysicalApplicationPath + "Data\\zt\\", "primaryZT", ztList, subject);

        }
        else
        {
            TVPage1.Controls.Remove(ZtPanel);
            TVPage1.Controls.Add(CommonUtil.getNoDataTVVerticalFlowPanel("ZtPanel", true)); ;
        }

       List<me_tgsg_category> tgsgList =  subjectService.getPrimaryZsdById(subjectId);
       if (null != tgsgList && tgsgList.Count != 0)
       {
           ZsdToXML(Request.PhysicalApplicationPath + "Data\\zt\\", "primaryZsd", tgsgList, subject);
       }
       else
       {
           TVPage1.Controls.Remove(zsdPanel);
           TVPage1.Controls.Add(CommonUtil.getNoDataTVVerticalFlowPanel("zsdPanel", true));
       }
    }

    private void ZtToXML(string path, string fileName, List<me_zt_baseinfo> ztList,string subject)
    {
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(dec);
        XmlElement root = doc.CreateElement("Data");
        doc.AppendChild(root);
        XmlNode rowNode = doc.CreateElement("listitems");
        root.AppendChild(rowNode);

        foreach (me_zt_baseinfo zt in ztList)
        {
            XmlElement entry = doc.CreateElement("entry");
            entry.SetAttribute("url", "ztDetail.aspx?zhztinfoid=" + zt.Zhztinfoid + "&subject=" + subject+"&headLine="+zt.Headline);
            entry.SetAttribute("iconUrl", zt.Backgroundpic);
            rowNode.AppendChild(entry);
        }
        doc.Save(path + "\\" + fileName + ".xml");
    }


    private void ZsdToXML(string path, string fileName, List<me_tgsg_category> tgsgList,string subject)
    {

        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(dec);
        XmlElement root = doc.CreateElement("listitems");
        doc.AppendChild(root);
        XmlNode rowNode = doc.CreateElement("Row");
        root.AppendChild(rowNode);

        try
        {

            foreach (me_tgsg_category tgsg in tgsgList)
            {
                XmlElement entry = doc.CreateElement("Entry");
                entry.SetAttribute("url", "primaryTopic.aspx?subjectId=" + tgsg.Subjectid + "&selId=" + tgsg.Id + "&subjectName=" + subject);
                entry.SetAttribute("title", CommonUtil.subEllipsisText(tgsg.Name, 30));
                entry.SetAttribute("detail", "授课老师:" + tgsg.Teachername + "\n" + "课程时长:" + tgsg.Lessontime + "\n" + "课程介绍:" + CommonUtil.replaceLineText(tgsg.Description));
                entry.SetAttribute("imgUrl", tgsg.Picture);
                rowNode.AppendChild(entry);
            }

            doc.Save(path + "\\" + fileName + ".xml");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    private void logAction()
    {
        string subjectId = (string)Session[CommonUtil.SESSION_SUBJECT_ID];
        string gradeId = (string)Session[CommonUtil.SESSION_GRADE_ID];
        me_tgsg_user user = (me_tgsg_user)Session[CommonUtil.SESSION_USER];
        UserService userService = new UserService();
        string url = Request.Url.AbsolutePath;
        userService.logUserView(user, url, CommonUtil.LOGIN, null, gradeId, subjectId);
    }
}